package arbor.mining.util.stock;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.StringTokenizer;

/**
 * A StockEntity is a k-line like entity, whatever it is a 30-min, 60-min or day. 
 * @author ktchuang
 */
public class StockEntity {
	public static final int RANGE_TYPE_INSTANT = 1;
	public static final int RANGE_TYPE_30MIN = 2;
	public static final int RANGE_TYPE_60MIN = 3;
	public static final int RANGE_TYPE_DAY = 4;	
	public static final int RANGE_TYPE_WEEK = 5;
	public static final int RANGE_TYPE_MONTH = 6;	
	
	public int rangeType;
	public GregorianCalendar startTime;
	public GregorianCalendar endTime;
	public OHLC ohlc;
	public int volume;
	public double adx = 0;
	public HashMap<String,Double> techIndexMap = null;
  
	public StockEntity(int rangeType, GregorianCalendar startTime, GregorianCalendar endTime) {
		this.rangeType = rangeType;
		this.startTime = startTime;
		this.endTime = endTime;
		ohlc = new OHLC();
	}
  
	public String toString() {
		DateFormat df = new SimpleDateFormat("yyyy/MM/dd-hh:mm:ss");
		StringBuffer buf = new StringBuffer();
		buf.append(df.format(startTime.getTime())+"~"+df.format(endTime.getTime()));
		buf.append("  ["+ ohlc.open+" 	");
		buf.append(ohlc.high+" 	");
		buf.append(ohlc.low+" 	");
		buf.append(ohlc.close+" 	");
		buf.append(volume+"]");
		return buf.toString();
	}
  
	/**
	 * follow the format:
	 * date open high low close vol
	 * e.g.,
	 * 1999/01/05 	6150.0 	6430.0 	6074.0 	6111.0 	5058
	 * @param line
	 * @return
	 * @throws ParseException 
	 */
	public static StockEntity parseEntity(String line) throws ParseException {
		StockEntity entity = new StockEntity(RANGE_TYPE_INSTANT, null, null);
		DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
		StringTokenizer token = new StringTokenizer(line, " 	");
		entity.startTime = new GregorianCalendar();
		entity.endTime = new GregorianCalendar();
		entity.startTime.setTime(df.parse(token.nextToken()));
		entity.endTime.setTime(entity.startTime.getTime());
		entity.ohlc.open = Double.parseDouble(token.nextToken());
		entity.ohlc.high = Double.parseDouble(token.nextToken());
		entity.ohlc.low = Double.parseDouble(token.nextToken());
		entity.ohlc.close = Double.parseDouble(token.nextToken());
		entity.volume = Integer.parseInt(token.nextToken());
		return entity;
	}    
	
	/**
	 * follow the format:
	 * date,time,open,high,low,close,vol
	 * e.g.,
	 * 1999/01/05,09:05,6430,6430,6350,6360,303
	 * @param line
	 * @return
	 * @throws ParseException 
	 */
	public static StockEntity parseMinuteEntity(String line) throws ParseException {
		StockEntity entity = new StockEntity(RANGE_TYPE_INSTANT, null, null);
		DateFormat df = new SimpleDateFormat("yyyy/MM/dd,hh:mm");
		DateFormat tf = new SimpleDateFormat("");
		
		StringTokenizer token = new StringTokenizer(line, ",");
		entity.startTime = new GregorianCalendar();
		entity.endTime = new GregorianCalendar();
		String time = token.nextToken()+","+token.nextToken();
		
		entity.startTime.setTime(df.parse(time));
		
		entity.endTime.setTime(entity.startTime.getTime());
		entity.ohlc.open = Double.parseDouble(token.nextToken());
		entity.ohlc.high = Double.parseDouble(token.nextToken());
		entity.ohlc.low = Double.parseDouble(token.nextToken());
		entity.ohlc.close = Double.parseDouble(token.nextToken());
		entity.volume = Integer.parseInt(token.nextToken());
		return entity;
	}  	
}